Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Basics overview

Python Indentation

What is Indentation?

Indentation refers to adding white space before a statement to a particular block of code. All the statements with the same space to the right belong to the same code block. Python uses indentation to highlight the blocks of code.

Rules of Indentation in Python:

✦Python’s default indentation spaces are four spaces. ✦The number of spaces, however, is entirely up to the user. ✦However, a minimum of one space is required to indent a statement. ✦Indentation is not permitted on the first line of Python code. ✦Python requires indentation to define statement blocks. Examples of Python Indentation: Here’s an example of Python indentation:
Python indentation importance with basic example # Python program showing indentation site = 'tutorialsbox' if site == 'tutorialsbox': print('Studying on tutorialsbox...') else: print('retype the URL.') print('All set !')

Output

Studying on tutorialsbox... All set !
In this example, the lines print('Studying on tutorialsbox...') and print('retype the URL.') are two separate code blocks. The two blocks of code in our if-statement are both indented four spaces.

Why is Indentation Important in Python?

Indentation is a very important concept in Python because without properly indenting the Python code, you will end up seeing IndentationError and the code will not get compiled. Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code. Remember, Python uses indentation to improve the readability of code and make it consistent across the wide spectrum of Python code. Consistency within one module or function is the most important.

  📌TAGS

★python ★ Indentation

Tutorials